<template>
  <div>
    <!-- 面包屑导航栏 -->
    <!-- 使用面包屑组件 -->
    <bread :title="title"></bread>
    <!-- 卡片包裹标签 -->
    <el-card class="box-card">
      <!-- 搜索按钮栏 -->
      <el-row :gutter="20">
        <!-- 搜索栏 -->
        <search @funQuery="search"></search>
        <el-col :span="6">
          <!-- 按钮 -->
          <el-button type="primary" @click="goAddpage">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 展示列表内容 -->
      <el-table :data="goodsList" style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" prop="add_time">
          <template slot-scope="scope">{{scope.row.add_time | datefmt('YYYY-MM-DD HH:mm:ss')}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteGoods(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页操作 -->
      <date1 :total="total" @funcF="show" @funcF2="show2"></date1>
    </el-card>
  </div>
</template>
           
<script>
//导入组件
import bread from "../common/breadcrumb.vue";
import date1 from "@/components/common/date.vue";
import search from "@/components/common/search.vue";
import { goodsList_api,deleteGoods } from "@/api";
export default {
  created() {
    this.getGoodsData();
  },
  data() {
    return {
      //传给子级的参数
      title: { one1: "商品管理", two2: "商品列表" },
      //步骤条显示第几步操作
      active: 0,
      goodsParams: {
        query: "",
        pagenum: 1,
        pagesize: 1
      },
      //商品列表信息
      goodsList: [],
      total: 0
    };
  },
  methods: {
    //获取商品列表
    async getGoodsData() {
      var { data: res } = await goodsList_api(this.goodsParams);
      if (res.meta.status !== 200)
        return this.$message.error("获取商品列表失败！");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    //分页功能
    show(size) {
      this.goodsParams.pagesize = size;
      this.getGoodsData();
    },
    show2(num) {
      this.goodsParams.pagenum = num;
      this.getGoodsData();
    },
    goAddpage() {
      this.$router.push("/goods/add");
    },
    search(val) {
      this.goodsParams.query = val;
      this.getGoodsData();
    },
    //删除商品
    async deleteGoods(id){
      var {data:res} = await deleteGoods(id);
      if(res.meta.status !== 200){
        return this.$message.error('删除商品失败！');
      }
      this.getGoodsData();
    }
  },
  //挂载组件名
  components: {
    bread,
    date1,
    search
  }
};
</script>
<style scoped>
</style>